﻿using System;
using System.Data;

using Model.Data;
using DataAccess;
using BusinessLogic.BusinessRules;
using BusinessLogic.ClassFactory;

namespace BusinessLogic.BusinessRules
{
    /// <summary>
    /// SectionRelation 的摘要说明。
    /// </summary>
    public class SectionRelation : BaseClass
    {
        #region 必有属性
        public Model.Data.SectionRelationData.SectionRelationRow DataItem
        {
            get
            {
                return (Model.Data.SectionRelationData.SectionRelationRow)base.m_CurrentRow;
            }
            set
            {
                base.m_CurrentRow = value;
            }
        }

        private Model.Data.SectionRelationData m_Data
        {
            get
            {
                return (Model.Data.SectionRelationData)base.m_DataSet;
            }
            set
            {
                base.m_DataSet = value;
            }
        }


        public override int Count
        {
            get
            {
                return this.m_Data.SectionRelation.Count;
            }
        }
        #endregion

        #region 构造函数
        public SectionRelation(DataAccess.DBManager m_DBManager, DataAccess.BaseAccess m_Access)
            : base(m_DBManager, m_Access)
        {
        }
        #endregion

        #region 必须重载方法
        /// <summary>
        /// 根据数据集中索引,设置当前数据行
        /// </summary>
        /// <param name="n_index">数据集中的索引</param>
        public override void SelectRow(int n_index)
        {

            if (m_Data != null && m_Data.SectionRelation.Rows.Count > n_index)
            {
                DataItem = m_Data.SectionRelation[n_index];
            }
        }


        /// <summary>
        /// 实现更新
        /// </summary>
        /// <returns></returns>
        protected override int OnUpdate()
        {
            return m_Access.Update(this.m_DataSet);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        protected override bool OnDelete(int ID)
        {
            return true;
        }


        public override void NewDataSet()
        {

            this.m_Data = new SectionRelationData();
            m_CurrentRow = m_Data.SectionRelation.NewSectionRelationRow();
        }

        public override void SetDataSet(DataSet m_DataSet)
        {
            this.m_DataSet = m_DataSet;
        }

        public override bool OnInsert()
        {
            this.DataItem.intSRID = this.GetNextSeed();
            this.m_Data.SectionRelation.Rows.Add(this.m_CurrentRow);
            return true;
        }
        #endregion

        #region 业务属性
        public Model.Data.SectionRelationData Data
        {
            get
            {
                return (Model.Data.SectionRelationData)base.m_DataSet;
            }
            set
            {
                base.m_DataSet = value;
            }
        }
        #endregion


        /// <summary>
        /// 返回引用指定栏目的栏目ID数组
        /// </summary>
        /// <param name="intSTID"></param>
        /// <returns>null值表示指定栏目未被任何栏目引用</returns>
        public static int[] GetReferedSectionIDs(int intSTID)
        {
            using (SectionRelationFactory objFactory = new SectionRelationFactory())
            {
                SectionRelation objSR = objFactory.CreateReferedSectionBySTID(intSTID);
                if (objSR == null)
                {
                    return null;
                }
                int[] aryReturn = new int[objSR.Count];
                for (int i = 0; i < objSR.Count; i++)
                {
                    objSR.SelectRow(i);
                    aryReturn[i] = objSR.DataItem.intSTID;
                }
                return aryReturn;
            }
        }
    }
}
